package Client;

import SQL.Connections;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.*;
import java.util.Vector;

public class PassengerHome extends JFrame{
    private JFrame frame;
    private JPanel panel1;
    private JButton 站站查询Button;
    private JButton 购票记录Button;
    private JButton 查询Button;
    private JButton 购票Button;
    private JTable table1;
    private JButton 修改密码Button;
    private JFormattedTextField formattedTextField1;
    private JFormattedTextField formattedTextField2;
    public static JFrame f;
    static String s[];

    private static String user;
    public static String user1;
    private Connection con;
    private Statement stm;
    private ResultSet rs;
    private Connections connections = new Connections();
    Vector<Vector> values;
    Vector<String> name;
    public String classno;

    //得到界面地址
    private  JFrame getFrame() {
        return frame;
    }

    public PassengerHome() {
        购票Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                    //跳转

                    Ticket ticket = new Ticket();
                    Ticket.initialize(classno);
                    frame.dispose();

            }
        });
        购票记录Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //跳转
                TicketRecord ticketRecord=new TicketRecord();
                ticketRecord.initialize(user1);
                frame.dispose();
            }
        });


        table1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                int rowINdex=table1.getSelectedRow();
                int Column=table1.getColumnCount();
                classno= (String) values.get(rowINdex).get(0);

            }
        });
        修改密码Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                ChangePassword changePassword=new ChangePassword();
                changePassword.initialize(user);
                frame.dispose();
            }
        });
    }

    public void check(String departure,String goal){

        try{
            Connection con = connections.getConnection();
            stm = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);
            //站站查询 获取数据库信息

            rs = stm.executeQuery("SELECT * FROM classes WHERE ClassDepaturePlace='"+departure+"' and ClassGoalPlace='"+goal+"'");
            int count=0;
            rs.first();
            while(rs.next()){
                count++;
            }

            ResultSetMetaData rsd=rs.getMetaData();
            String[] name1=new String[rsd.getColumnCount()];
            String[][] values1 =new String[count+1][rsd.getColumnCount()];
            rs.first();


            int i=0;

            while (rs.next()){//数据更新

                for (int j = 1; j <= rsd.getColumnCount(); j++) {
                    values.get(i).set(j - 1, rs.getString(j));
                }
                i += 1;
            }
            for(i=1;i<=rsd.getColumnCount();i++) name1[i - 1] = rsd.getColumnName(i);//数据名字


            //更新表格
            DefaultTableModel model=new DefaultTableModel(values,name);
            table1.setModel(model);


            rs.close();
            stm.close();
            con.close();
        }catch (Exception e){
            e.printStackTrace();

        }
    }
    public static void initialize(String user) {

        /*
        JFrame frame = new JFrame("PassengerHome");
        frame.setContentPane(new PassengerHome().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);//设置屏幕居中
        frame.setVisible(true);
        f=frame;
        */
        //创建普通用户界面
        PassengerHome passengerHome = new PassengerHome();
        //保存普通用户界面的地址
        passengerHome.frame = passengerHome;
        //设置用户界面的标题
        passengerHome.setTitle("用户界面");
        //设置窗口的基础面板，显示窗口内容
        passengerHome.setContentPane(passengerHome.panel1);
        passengerHome.setDefaultCloseOperation(passengerHome.EXIT_ON_CLOSE);
        passengerHome.pack();
        //设置屏幕居中
        passengerHome.setLocationRelativeTo(null);
        //设置用户界面为可见
        passengerHome.setVisible(true);

        //保存用户名
        PassengerHome.user = user;
        user1=new String(user);
    }

    public static void initialize() {

        PassengerHome.initialize(user);

    }

    public  void createUIComponents() {
        // TODO: place custom component creation code here





            String sql=("SELECT * FROM classes");
            values=new Vector<>();
            name=new Vector();
            values=Connections.queryData(sql);
            name.add("班次号");
            name.add("火车号");
            name.add("出发地点");
            name.add("目的地");
            name.add("出发时间");
            name.add("可载总数");
            name.add("距离");
            TableModel d=new DefaultTableModel(values,name);
            table1= new JTable(values,name);
            table1.setRowSelectionInterval(0,0);

    }


    public static void main(String[] args) {
            initialize();
    }
}
